package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityAuditLogMapper;
import com.ztgf.commodity.models.bo.CommodityAuditLogSaveBO;
import com.ztgf.commodity.models.dto.CommodityAuditLogDTO;
import com.ztgf.commodity.models.service.ICommodityAuditLogService;
import com.ztgf.commodity.models.vo.CommodityAuditLogVO;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.user.models.dto.UserInfoDTO;
import com.ztgf.user.models.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-26 16:51
 * Description:
 */
@Slf4j
@DubboService
public class CommodityAuditLogServiceImpl extends ServiceImpl<CommodityAuditLogMapper, CommodityAuditLogDTO> implements ICommodityAuditLogService {

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IUserInfoService userInfoService;
    @DubboReference
    private ISysUserService sysUserService;

    @Async
    @Override
    public void add(CommodityAuditLogSaveBO bo) {
        CommodityAuditLogDTO dto = new CommodityAuditLogDTO();
        BeanUtils.copyProperties(bo, dto);
        dto.setCreateUserId(bo.getAdminId());
        dto.setId(ymlConfig.getPkId());
        boolean insert = this.insert(dto);
        if (insert) {
            log.info("保存商品审核日志成功!");
        } else {
            log.error("保存商品审核日志失败!");
        }
    }

    @Override
    public List<CommodityAuditLogVO> queryCommodityAuditLogList(CommodityAuditLogVO vo) {
        Page<CommodityAuditLogVO> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        return baseMapper.queryCommodityAuditLogList(page, vo);
    }

    @Override
    public CommodityAuditLogVO getByLogId(Long id) {
        return baseMapper.getByLogId(id);
    }

    /**
     * 根据商品id获取审核记录列表
     * @param id
     * @return
     */
    @Override
    public List<CommodityAuditLogVO> getLogsByCommodityId(Long id) {
        ArrayList<CommodityAuditLogVO> voList = new ArrayList<CommodityAuditLogVO>();

        Wrapper<CommodityAuditLogDTO> wrapper = new EntityWrapper<> ();
        wrapper.eq("commodity_id", id);
        wrapper.orderBy("id", true);
        List<CommodityAuditLogDTO> dtoList = baseMapper.selectList(wrapper);
        for (CommodityAuditLogDTO dto : dtoList){
            CommodityAuditLogVO vo = new CommodityAuditLogVO();
            dto2vo(dto, vo);
            voList.add(vo);
        }
        return voList;
    }

    private void dto2vo(CommodityAuditLogDTO dto, CommodityAuditLogVO vo) {
        BeanUtils.copyProperties(dto, vo);
        UserInfoDTO userInfoDTO = userInfoService.selectById(dto.getCreateUserId());
        if (userInfoDTO != null){
            vo.setUserName(userInfoDTO.getContactName());
        }else {
            SysUserDTO sysUserDTO = sysUserService.selectById(dto.getCreateUserId());
            if (sysUserDTO != null){
                vo.setUserName(sysUserDTO.getName());
            }
            else {
                vo.setUserName("");
            }
        }
    }
}
